﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.Initializer"/>.
    /// </summary>
    public class InitializerVM : ExpressionVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(Initializer),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new InitializerVM((Initializer)codeObject, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ChildListVM<ExpressionVM> _expressionVMs;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.Initializer"/>.
        /// </summary>
        public InitializerVM(Initializer initializer, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(initializer, dictionary)
        {
            _expressionVMs = CreateListVM<Expression, ExpressionVM>(initializer.Expressions, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.Initializer"/> model.
        /// </summary>
        public Initializer Initializer
        {
            get { return (Initializer)CodeObject; }
        }

        /// <summary>
        /// A collection of child <see cref="ExpressionVM"/>s.
        /// </summary>
        public ChildListVM<ExpressionVM> Expressions
        {
            get { return _expressionVMs; }
        }

        /// <summary>
        /// True if there are any child <see cref="Expression"/>s.
        /// </summary>
        public bool HasExpressions
        {
            get { return (_expressionVMs != null && _expressionVMs.Count > 0); }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override void Render(CodeRenderer renderer, RenderFlags flags)
        {
            flags |= RenderFlags.ForceBorder;

            int newLines = Initializer.NewLines;
            if (newLines > 0)
            {
                if (!flags.HasFlag(RenderFlags.SuppressNewLine))
                    renderer.NewLines(newLines, ParentVM);
            }
            else if (flags.HasFlag(RenderFlags.PrefixSpace))
                renderer.RenderText(" ", PUNC_BRUSH, ParentVM);

            bool increaseIndent = (flags.HasFlag(RenderFlags.IncreaseIndent) && !flags.HasFlag(RenderFlags.NoIncreaseIndent));
            bool hasBorder = (flags.HasFlag(RenderFlags.ForceBorder) || HasBorder()) && !flags.HasFlag(RenderFlags.NoBorder);
            if (hasBorder)
            {
                CreateBorder(renderer, flags);
                increaseIndent = true;  // Always increase the indent level for wrapped lines inside a border
            }

            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            RenderBefore(renderer, passFlags | RenderFlags.IsPrefix);

            // Increase the indent level for any newlines that occur within the initializer if the flag is set
            if (increaseIndent)
                renderer.IndentOnNewLineBegin(this);

            RenderExpression(renderer, passFlags | (flags & (RenderFlags.Attribute | RenderFlags.HasDotPrefix | RenderFlags.Declaration)));
            if (Initializer.HasTerminator && !StatementVM.HideTerminators)
                renderer.RenderText(Statement.ParseTokenTerminator, PUNC_BRUSH, this);
            RenderEOLComments(renderer, flags);

            if (increaseIndent)
                renderer.IndentOnNewLineEnd(this);

            RenderAfter(renderer, passFlags | (flags & RenderFlags.NoPostAnnotations));

            // If we have a border, return to the previous one
            if (hasBorder)
                renderer.ReturnToBorderParent(this);
        }

        public override void RenderExpression(CodeRenderer renderer, RenderFlags flags)
        {
            // Render the open brace
            if (!BlockVM.HideBraces)
            {
                renderer.RenderText(Initializer.ParseTokenStart, PUNC_BRUSH, CodeRenderer.CodeFontFamily,
                    BlockVM.TinyBraces ? CodeRenderer.TinyFontSize : CodeRenderer.CodeFontSize, 0, TextStyle.Normal, this);
                flags &= ~RenderFlags.SuppressNewLine;
            }

            if (!flags.HasFlag(RenderFlags.NoEOLComments))
                RenderInfixEOLComments(renderer, flags);

            // Render the body of the initializer.  Always prefix spaces, even on the first item after the '{' (ignored
            // for IsFirstOnLine items).  Force borders on all expressions if the Initializer is multi-line.
            renderer.RenderList(_expressionVMs, flags | RenderFlags.PrefixSpace
                | (_expressionVMs != null && _expressionVMs.Count > 0 && _expressionVMs[0].IsFirstOnLine ? RenderFlags.ForceBorder : 0), this);

            // Render the close brace
            if (!BlockVM.HideBraces)
            {
                int endNewLines = Initializer.EndNewLines;
                if (endNewLines > 0)
                    renderer.NewLines(endNewLines, ParentVM);
                else
                    renderer.RenderText(" ", PUNC_BRUSH, ParentVM);

                renderer.RenderText(Initializer.ParseTokenEnd, PUNC_BRUSH, CodeRenderer.CodeFontFamily,
                    BlockVM.TinyBraces ? CodeRenderer.TinyFontSize : CodeRenderer.CodeFontSize, 0, TextStyle.Normal, this);
            }
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            renderer.RenderVisibleList(_expressionVMs, flags);
        }

        public override void UnRender()
        {
            ChildListHelpers.UnRender(_expressionVMs);
            base.UnRender();
        }

        #endregion
    }
}
